package com.earthea.application.web.controller.tool;

import com.earthea.application.common.core.controller.BaseController;
import com.earthea.application.common.entity.BaseResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * swagger 用户测试方法
 */
@RestController
@RequestMapping("/rest/test/user")
@Tag(name = "用户信息管理", description = "用户信息管理相关的接口")
public class TestController extends BaseController {
    private final static Map<Integer, UserEntity> users = new LinkedHashMap<Integer, UserEntity>();

    {
        users.put(1, new UserEntity(1, "admin", "admin123", "15888888888"));
        users.put(2, new UserEntity(2, "zhysunny", "zhysunny123", "15666666666"));
    }

    @Operation(summary = "获取用户列表")
    @GetMapping("/list")
    public BaseResponse userList() {
        List<UserEntity> userList = new ArrayList<UserEntity>(users.values());
        BaseResponse response = BaseResponse.success();
        response.setData(userList);
        return response;
    }

    @Operation(summary = "获取用户详细")
    @GetMapping("/{userId}")
    public BaseResponse getUser(@PathVariable Integer userId) {
        if (!users.isEmpty() && users.containsKey(userId)) {
            BaseResponse response = BaseResponse.success();
            response.setData(users.get(userId));
            return response;
        } else {
            return BaseResponse.failed("用户不存在");
        }
    }

    @Operation(summary = "新增用户")
    @PostMapping("/save")
    public BaseResponse save(UserEntity user) {
        if (!Optional.ofNullable(user).map(UserEntity::getUserId).isPresent()) {
            return BaseResponse.failed("用户ID不能为空");
        }
        users.put(user.getUserId(), user);
        return BaseResponse.success();
    }

    @Operation(summary = "更新用户")
    @PutMapping("/update")
    public BaseResponse update(@RequestBody UserEntity user) {
        if (!Optional.ofNullable(user).map(UserEntity::getUserId).isPresent()) {
            return BaseResponse.failed("用户ID不能为空");
        }
        if (users.isEmpty() || !users.containsKey(user.getUserId())) {
            return BaseResponse.failed("用户不存在");
        }
        users.remove(user.getUserId());
        users.put(user.getUserId(), user);
        return BaseResponse.success();
    }

    @Operation(summary = "删除用户信息")
    @DeleteMapping("/{userId}")
    public BaseResponse delete(@PathVariable Integer userId) {
        if (!users.isEmpty() && users.containsKey(userId)) {
            users.remove(userId);
            return BaseResponse.success();
        } else {
            return BaseResponse.failed("用户不存在");
        }
    }
}

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Schema(description = "用户实体")
class UserEntity {
    @Schema(description = "用户ID", example = "1001")
    private Integer userId;

    @Schema(description = "用户名称", example = "张三")
    private String username;

    @Schema(description = "用户密码", example = "123456")
    private String password;

    @Schema(description = "用户手机", example = "15288888888")
    private String mobile;
}
